// import { gotoLogin } from '@/model/authToLogin/auth'
import { stringifyParams } from '@/util/util'
import { env } from '@/config/config'
import store from '@/store/store'
async function navigate(config: Page.ITurnPageConfig) {
  console.log('navigate')

  const { userState, tabBarState } = store
  config.navMethod ||= 'navigate'
  const queryStr = config.query ? stringifyParams(config.query) : ''
  const url = queryStr ? `${config.url}?${queryStr}` : config.url
  const tabBarPages = tabBarState.tabBarList.reduce(
    (memo: any, item: any) => [...memo, `/${item.pagePath}`],
    [] as string[]
  )
  switch (config.navMethod) {
    case 'navigateBack': {
      const pages = getCurrentPages()
      if (pages.length > 1) {
        uni.navigateBack({})
        if (config?.query) {
          uni.$emit('refreshPreviousPage', { data: config?.query })
        }
      } else {
        uni.reLaunch({ url: '/pages/home3/index' })
      }
      break
    }
    case 'navigate': {
      if (url.includes('/pages/download/index')) {
        const userToken_zh_web = uni.getStorageSync('userToken_zh_web')
        const reUrl = url.includes('?') ? url + '&t=' + userToken_zh_web.token : url + '?t=' + userToken_zh_web.token
        uni.navigateTo({ url: reUrl })
      } else {
        uni.navigateTo({ url })
      }
      break
    }
    case 'redirect': {
      uni.redirectTo({ url })
      break
    }
    case 'reLaunch': {
      uni.reLaunch({ url })
      break
    }
    case 'switchTab': {
      uni.switchTab({ url: config.url || '/' })
      break
    }
  }
  if (tabBarPages.includes(config.url)) {
    tabBarState.currentTab = config.url?.slice(1)
  }
}

export { navigate }
